package catalina.connection;

import catalina.startup.LifeCycle;
import catalina.startup.StandardService;

import java.net.ServerSocket;
import java.util.Properties;
import java.util.concurrent.*;

public class Connector implements LifeCycle {

    public void setStandardService(StandardService standardService) {
        this.standardService = standardService;
    }

    public StandardService getStandardService() {
        return standardService;
    }

    private NioEndPoint nioEndPoint;

    private StandardService standardService;

    private Properties properties;

    /**定义socket监听的端口号*/
    private int port = 8080;

    private int maximumPoolSize =200;

    private int minSpareThreads = 10;

    private ServerSocket serverSocket;

    private ThreadPoolExecutor threadPoolExecutor;

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    @Override
    public void init() {
//        if (properties != null) {
//            this.port = Integer.valueOf(properties.get("port").toString());
//            Object executor = properties.get("Executor");
//            if (executor != null && executor instanceof Properties) {
//                Properties executorProperties = (Properties) executor;
//               this.maximumPoolSize= Integer.valueOf(executorProperties.get("maxThreads").toString());
//                this.minSpareThreads=Integer.valueOf(executorProperties.get("minSpareThreads").toString());
//            }
//        }
//        BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(100);
//        ThreadFactory threadFactory = Executors.defaultThreadFactory();
//        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
//        this.threadPoolExecutor = new ThreadPoolExecutor(
//                minSpareThreads,
//                maximumPoolSize,
//                60L,
//                TimeUnit.SECONDS,
//                workQueue,
//                threadFactory,
//                handler
//        );
//        try {
//            this.serverSocket = new ServerSocket(port);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        System.out.println("=====>>>Minicat start on port：" + port);

        if (nioEndPoint == null) {
            nioEndPoint = new NioEndPoint(this);
        }
        if (properties != null) {
            Integer port = Integer.valueOf(properties.get("port").toString());
            nioEndPoint.setPort(port);
            Object executor = properties.get("Executor");
            if (executor != null && executor instanceof Properties) {
                Properties executorProperties = (Properties) executor;
                nioEndPoint.setMaxThreads(Integer.valueOf(executorProperties.get("maxThreads").toString()));
                nioEndPoint.setMinSpareThreads(Integer.valueOf(executorProperties.get("minSpareThreads").toString()));
            }
        }
        nioEndPoint.init();
    }

//    public class connectWorker extends Thread{
//        private ThreadPoolExecutor executor;
//
//        private StandardService standardService;
//        public connectWorker(ThreadPoolExecutor executor,StandardService standardService){
//            this.executor = executor;
//            this.standardService = standardService;
//        }
//        @Override
//        public void run() {
//            while(true) {
//                Socket socket = null;
//                try {
//                    socket = serverSocket.accept();
//                    RequestProcessor requestProcessor = new RequestProcessor(socket,standardService);
//                    this.executor.execute(requestProcessor);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }

    @Override
    public void start() {
//        new connectWorker(this.threadPoolExecutor,standardService).start();
        if (nioEndPoint != null) {
            nioEndPoint.start();
        }
    }

    @Override
    public void stop() {

    }

    @Override
    public void destroy() {

    }
}